const {
	exec,
	escape
} = require('../mysql.js')
const {
	formatTime
} = require('../../utils/time.js')

function getBlogs(page) {
	let {
		pageSize,
		pageNo
	} = page
	if (!pageNo) pageNo = 1
	if (!pageSize) pageSize = 10
	
	pageNo = escape(Number(pageNo))
	pageSize = escape(Number(pageSize))
	let a = (Number(pageNo) - 1) * Number(pageSize)
	// let a = escape((pageNo-1)*pageSize)
	let sql =
		`SELECT * FROM blogs WHERE user_id = 37 ORDER BY create_time DESC LIMIT ${a},${pageSize};`
	return exec(sql)
}


function getBlogDetail(id) {
	id = escape(id)
	let sql =
		`SELECT * FROM blogs WHERE user_id = 37 and id = ${id};`
	return exec(sql)
}

// 修改博客
function editBlog(data) {
	let {
		id,
		title,
		content,
		tag,
		topImg
	} = data
	let time = new Date()
	let editTime = formatTime(time)
	if (!id) {
		return false
	}
	let sql = `update blogs set `
	if (title && content && tag && topImg) {
		sql +=
			`title = ${escape(title)},content = ${escape(content)},tag = ${escape(tag)},top_img = ${escape(topImg)},edit_time = ${escape(editTime)} where id = ${escape(id)}`
	}else{
		return false
	}
	return exec(sql)
}

// 添加博客
function createBlog(data){
	
}

module.exports = {
	getBlogs,
	getBlogDetail,
	editBlog
}
